package com.mdd.front.controller;

import com.alibaba.fastjson2.JSONObject;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.core.WxResult;
import com.mdd.common.enums.CodeEnum;
import com.mdd.common.enums.HttpEnum;
import com.mdd.common.util.OrderUtils;
import com.mdd.front.LikeFrontThreadLocal;
import com.mdd.front.vo.annotation.PreventRepeatSubmit;
import com.mdd.front.service.IOrderService;
import com.mdd.front.validate.commons.PageValidate;
import com.mdd.front.validate.order.OrderCreateValidate;
import org.springframework.http.HttpHeaders;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

/**
 * 订单控制层
 * @author shenjian
 * @createTime 2023年02月20日 22:58:00
 */
@RestController
@Valid
@RequestMapping("api/order")
public class OrderController {

    @Resource
    IOrderService iOrderService;

    /**
     * 创建订单
     * @param orderCreateValidate
     * @return
     */
    @PostMapping("/create")
    public AjaxResult<Object> create(@Validated @RequestBody OrderCreateValidate orderCreateValidate) {
        // 登录人员
        Integer userId = LikeFrontThreadLocal.getUserId();
        // 生成订单号
        String orderNum = OrderUtils.getInstance().generateOrder(CodeEnum.ORDER_CODE, userId);
        orderCreateValidate.setOrderNo(orderNum);
        iOrderService.createOrder(orderCreateValidate, userId);
        return AjaxResult.success(HttpEnum.SUCCESS.getMsg(),orderNum);
    }

    /**
     * 根据订单号轮询
     * @param orderNo
     * @return
     */
    @GetMapping("/query-order-no")
    public AjaxResult<Object> queryOrderNo(@RequestParam(value = "order_no")
                                           @NotBlank(message = "订单号不能为空") String orderNo) {
        return iOrderService.queryWxPrePayId(orderNo);
    }

    /**
     * 微信支付回调
     * @param jsonObject
     * @param headers
     * @return
     */
    @PostMapping("/pay-callback")
    public WxResult payCallback(@RequestBody JSONObject jsonObject,
                                @RequestHeader HttpHeaders headers) {
        return iOrderService.payCallback(jsonObject, headers);
    }

    /**
     * 查询个人订单
     * @return
     */
    @GetMapping("/search-order")
    public AjaxResult<Object> search(@Validated PageValidate pageValidate,
                                     @RequestParam(value = "status", defaultValue = "1") Integer status) {
        return AjaxResult.success(iOrderService.searchOrderList(pageValidate, status));
    }

    /**
     * 查询订单详情
     */
    @GetMapping("/find-order-by-id")
    public AjaxResult<Object> findOrderById(@RequestParam(value = "order_id")
                                            @NotBlank(message = "订单ID不能为空") Integer orderId) {
        return AjaxResult.success(iOrderService.findOrderById(orderId));
    }

    /**
     * 申请退款
     * @param orderId
     */
    @GetMapping("/apply-refund")
    @PreventRepeatSubmit
    public AjaxResult<Object> applyRefund(@RequestParam(value = "order_id")
                                          @NotBlank(message = "订单号不能为空") Integer orderId) {
        iOrderService.applyRefund(orderId);
        return  AjaxResult.success();
    }

    /**
     * 微信退款回调
     * @param jsonObject
     * @param headers
     * @return
     */
    @PostMapping("/refund-callback")
    public WxResult refundCallback(@RequestBody JSONObject jsonObject,
                                   @RequestHeader HttpHeaders headers) {
        return iOrderService.refundCallback(jsonObject, headers);
    }

    /**
     * 拉取微信支付，判断状态
     * @param orderNo
     * @return
     */
    @GetMapping("/pull-pay")
    public AjaxResult<Object> pullWxPay(@RequestParam(value = "order_no")String orderNo) {
        iOrderService.pullWxPay(orderNo);
        return AjaxResult.success();
    }
}
